package com.woniuxy.charin.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.common.ann.AuthAnnotation;
import com.woniuxy.charin.model.entity.Investors;
import com.woniuxy.charin.model.entity.InvestorsvoZb;
import com.woniuxy.charin.model.entity.Projects;
import com.woniuxy.charin.model.vo.InvestorsVo;
import com.woniuxy.charin.model.vo.RequestInvestorsVo;
import com.woniuxy.charin.service.InvestorsService;
import com.woniuxy.charin.service.InvestorsvoZbService;
import com.woniuxy.charin.service.ProjectsService;
import com.woniuxy.charin.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Investors")
@Api(tags = "投资商管理模块")
public class InvestorsController {
    @Autowired
    private InvestorsService investorsService;
    @Autowired
    private InvestorsvoZbService  investorsvoZbService;
    @Autowired
    private ProjectsService projectsService;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 条件查询
     */
    @PostMapping("/query")
    @ApiOperation("条件查询")
    public ResponseEntity<?>query(@RequestBody Map<String, String> params){

        Page<InvestorsVo> query=  investorsService.query(params);
        return ResponseEntity.ok(query);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/getById")
    @ApiOperation("根据id查询")
    public ResponseEntity<?>getById(Integer invId){
        Investors investors = investorsService.findById(invId);
        return ResponseEntity.ok(investors);
    }
    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    @ApiOperation("批量删除")
    public ResponseEntity<?>batchDelete(@RequestBody Integer[]invIds){
        investorsService.batchDelete(invIds);
        return ResponseEntity.ok(null);
    }



    /**
     * 根据id删除单个用户
     */
    @GetMapping("/deleteById")
    @ApiOperation("根据投资商id删除单个用户")
    @AuthAnnotation(value = "investors:delete")
    public ResponseEntity<?>deleteById(Integer invId){
        investorsService.deleteById(invId);
        return ResponseEntity.ok(null);
    }
    /**
     * 新增投资商
     */
    @PostMapping("/getAll")
    @ApiOperation("显示所有投资商")
    public ResponseEntity<?>getAll(@RequestBody Map<String, Integer> params){
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        Page<InvestorsVo> all = investorsService.getAll(pageNum, pageSize);
        return ResponseEntity.ok(all);

    }
    @PostMapping("/getInv")
    @ApiOperation("显示待处理投资商")
    public ResponseEntity<?>getInv(@RequestBody Map<String, Integer> params){
        //获取页码和页面大小
        int pageNum = params.get("pageNum");
        int pageSize = params.get("pageSize");
        Page<InvestorsVo> all = investorsService.getInv(pageNum, pageSize);
        return ResponseEntity.ok(all);

    }
    @GetMapping("/getInvByuser")
    public ResponseEntity<?>getInvByuser(@RequestHeader(required = false, name = "Authorization")String authorization){
        Integer userId= Integer.valueOf(jwtUtil.getValue(authorization, "userId"));
        List<InvestorsVo> all=  investorsService.getByU(userId);
        return ResponseEntity.ok(all);
    }
    /**
     * 编辑投资商
     */
    @PostMapping("/edit")
    @ApiOperation("编辑投资商")
    @Transactional
    public ResponseEntity<?>edit(@RequestBody RequestInvestorsVo requestInvestorsVo){
        Investors investors = requestInvestorsVo.getInvestors();
        investorsService.updateById(investors);
        List<InvestorsvoZb> investorsvoZbs = requestInvestorsVo.getInvestorsvoZbs();
        if (investorsvoZbs.size()>0){
            investorsvoZbs.forEach(item->{
                investorsvoZbService.updateById(item);
            });
        }
        List<Projects> projects = requestInvestorsVo.getProjects();
        if (projects.size()>0){
            projects.forEach(item->{
                projectsService.updateById(item);
            });
        }
        return ResponseEntity.ok().body(null);
    }
    /**
     * 新增投资商
     */
    @PostMapping("/addInvestors")
    @ApiOperation("新增投资商")
    @Transactional
    public ResponseEntity<?>addInvestors(@RequestBody RequestInvestorsVo requestInvestorsVo, @RequestHeader(required = false, name = "Authorization")String authorization){
        Investors investors = requestInvestorsVo.getInvestors();
        investors.setStatus("待处理");
        Integer userId = Integer.valueOf(jwtUtil.getValue(authorization, "userId"));
        investors.setUserId(userId);
         investorsService.save(investors);
        Integer id = investors.getId();
        List<InvestorsvoZb> investorsvoZbs = requestInvestorsVo.getInvestorsvoZbs();
        investorsvoZbs.forEach(item->{
            item.setInvestorsId(id);
            investorsvoZbService.save(item);
        });
        List<Projects> projects = requestInvestorsVo.getProjects();
        projects.forEach(item->{
            item.setInvestorsId(id);
            projectsService.save(item);
        });

        return ResponseEntity.ok().body(null);
    }
}
